/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdressBooks.java
 *
 * Created on 03.01.2012, 13:37:29
 */
package engine;

/**
 *
 * @author Alexx
 */
import appClient.ConnectClient;
import appClient.ErrorValidation;
import appClient.Forms;
import appClient.UserComman;
import appClient.VisualClien;
import com.google.gson.Gson;
import engine.Address;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultCellEditor;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import photo.AddPhoto;
import photo.ReadFile;
import protocol.ClientProtocol;
import protocol.Command;

public class AdressBooks extends javax.swing.JFrame implements Forms {
  private static boolean editMode = false;
  private int id;
  private FullPersone fp;
  private ClientProtocol clPr;
  private ConnectClient cli;
  private JTable adrTable;
  private JTable phonTable;
  private int phoneRow;
  private int adressRow;
  private String phone[][];
  private String adrr[][];
  public String[] items =  {"not"};
  public String[] adrType =  {"not"};
  public String[] countr =  {"not"};
  public String[] cit =  {"not"};
  public String[] stre =  {"not"};
  public List<Country> countryList;
  public List<City> cityList;

  /** Creates new form AdressBooks */
  public AdressBooks()
  {
    initComponents();
    emtptyTable();
    emtptyTableAdr();
  }
  
   public AdressBooks(ConnectClient cli)
  {
    initComponents();
    this.cli = cli;
    emtptyTable();
    emtptyTableAdr();
  }
  
  public AdressBooks(FullPersone fp, ClientProtocol clPr) throws FileNotFoundException, IOException
  {
    initComponents();
    this.fp = fp;
    this.clPr = clPr;
    setData(fp);
  }
  
  public void setLink()
  {
    cli.clientProtocol.ab = this;
  }

  public void setAdrType(String[] adrType) {
    this.adrType = adrType;
  }

  public void setCit(String[] cit) {
    this.cit = cit;
  }

  public void setCityList(List<City> cityList) {
    this.cityList = cityList;
  }

  public void setCountr(String[] countr) {
    this.countr = countr;
  }

  public void setCountryList(List<Country> countryList) {
    this.countryList = countryList;
  }

  public void setItems(String[] items) {
    this.items = items;
  }

  public void setStre(String[] stre) {
    this.stre = stre;
  }
  
  
  
  private boolean setData(FullPersone fp) throws FileNotFoundException, IOException
  {
      NameFild.setText(fp.getFName());
      LastNameField.setText(fp.getLNmae());
      AgeField.setText(fp.getBd());
      
      if(!clPr.connectClient.role.equals("Admin"))
      {
        if (!clPr.connectClient.Uid.equals(fp.uid)) {
          SaveButton.setVisible(false);
        }
      }

      setTel(fp);
      setAdr(fp);
      setPhoto(fp);
      return true;
  }
  
  private void setPhoto(FullPersone fp) throws FileNotFoundException, IOException
  {
      new File("src/foto/buf.jpg").delete();
      ReadFile rf = new ReadFile();
      rf.read(fp.ID, clPr.connectClient);
  }
  
  @Override
    public void setPhoto() {
      PhotoPanel.removeAll();
      PhotoPanel.add(new AddPhoto("src/foto/buf.jpg"));
      PhotoPanel.repaint();
      PhotoPanel.updateUI();
    }
    
  private void setTel(FullPersone fp) {
//          private void setTel()
//  {
//      fp = new FullPersone();
//      Phone p1 = new Phone();
//      p1.Number = "434-43-43";
//      TypePhone pt = new TypePhone();
//      pt.Type = "mob";
//      p1.Type = pt;
//      Phone p2 = new Phone();
//      p2.Number = "777-43-43";
//      TypePhone pt2 = new TypePhone();
//      pt2.Type = "Home";
//      p2.Type = pt2;
//      List<Phone> lp = new ArrayList<Phone>();
//      lp.add(p1);
//      lp.add(p2);
//      fp.Phones = lp;
              
              ///////////
      if (fp.Phones != null){
          phone = new String[fp.Phones.size()][3];
          phoneRow = fp.Phones.size();
          int count = 0;
          for (Phone ph : fp.Phones) {
              phone[count][0] = ph.Type.Type;
              phone[count][1] = ph.Number;
              phone[count][2] = ph.ID;
              count++;
          }
          phoneTable();
          
      }
      
      
  }
  private boolean emtptyTable()
  {
      phoneRow = 0;
      phone = new String[0][3];
      phoneTable();
      return true;
  }
  
  private boolean emtptyTableAdr()
  {
      adressRow = 0;
      adrr = new String[0][6];
      AdrTable();
      return true;
  }
  
  private boolean addTell()
  {
      int i = phone.length;
      i++;
      phone = new String[i][3];
      setTable();
      phoneRow = i;
      phone[i-1][1] = "Enter tel";
      phoneTable();
      phonTable.updateUI();
      return true;
  }
  
  private boolean addAdr()
  {
      int i = adrr.length;
      i++;
      adrr = new String[i][6];
      setTableAdr();
      adressRow = i;
      adrr[i-1][1] = "Enter adr";
      AdrTable();
      adrTable.updateUI();
      return true;
  }
  
  public boolean setTableAdr()
  {
      List<Address> Adrest = getAdress();
      int count = 0;
      for(Address p : Adrest)
      {
          adrr[count][0] = p.Type.Type;
          adrr[count][1] = p.country.name;
          adrr[count][2] = p.city.name;
          adrr[count][3] = p.street.name;
          adrr[count][4] = p.Add;
          adrr[count][5] = p.ID;
          count++;
      }
    return true;  
  }
  
  
  private boolean setTable()
  {
      List<Phone> Phones = getPhones();
      int count = 0;
      for(Phone p : Phones)
      {
          phone[count][0] = p.Type.Type;
          phone[count][1] = p.Number;
          phone[count][2] = p.ID;
          count++;
      }
    return true;  
  }
  
  public boolean phoneTable()
  {
      phonTable = new JTable();
      phonTable.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
      phonTable.setModel(new javax.swing.table.DefaultTableModel(phone, new String[]{"Type", "Tel", "ID"}));
      phonTable.setRowHeight(25);

        TableColumn type = phonTable.getColumn("Type");
        JComboBox cb = new JComboBox(items);
        type.setCellEditor(new DefaultCellEditor(cb));

      PhonePanel.setViewportView(phonTable);
      return true;
  }
  
  public boolean AdrTable()
  {
      adrTable = new JTable();
      adrTable.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
      adrTable.setModel(new javax.swing.table.DefaultTableModel(adrr, new String[]{"Type", "Country", "City", "Street", "Flat", "ID"}));
      adrTable.setRowHeight(25);
        
      TableColumn type = adrTable.getColumn("Type");
      JComboBox cb = new JComboBox(adrType);
      type.setCellEditor(new DefaultCellEditor(cb));
      
      TableColumn country = adrTable.getColumn("Country");
      final JComboBox cb2 = new JComboBox(countr);

      ActionListener actionListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JComboBox box = (JComboBox) e.getSource();
                String item = (String) box.getSelectedItem();
                getCity(item);
            }
        };
      cb2.addActionListener(actionListener);
      country.setCellEditor(new DefaultCellEditor(cb2));
      
      TableColumn city = adrTable.getColumn("City");
      JComboBox cb3 = new JComboBox(cit);
      
      ActionListener actionListener2 = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JComboBox box = (JComboBox) e.getSource();
                String item = (String) box.getSelectedItem();
                getStreet(item);
            }
        };
      cb3.addActionListener(actionListener2);
      city.setCellEditor(new DefaultCellEditor(cb3));
      
      TableColumn street = adrTable.getColumn("Street");
      JComboBox cb4 = new JComboBox(stre);
      street.setCellEditor(new DefaultCellEditor(cb4));


      AdressPanel.setViewportView(adrTable);
      return true;
  }
  
  private boolean getCity(String st)
  {
      String id = null;
      for (Country p : countryList)
      {
          if (p.name.equals(st)){
             id = p.ID;
          }
      }
      String res = UserComman.commandGet("City", id);
      if (fp != null) {
          clPr.connectClient.refuse(res);
      } else {
          cli.refuse(res);
      }
     return true; 
  }
  
  private boolean getStreet(String st)
  {
      String id = null;
      for (City p : cityList)
      {
          if (p.name.equals(st)){
             id = p.ID;
          }
      }
      String res = UserComman.commandGet("Street", id);
      if (id != null){
        if (fp != null) {
          clPr.connectClient.refuse(res);
        } else {
          cli.refuse(res);
        }
      }
      
     return true; 
  }
  
          
  private void setAdr(FullPersone fp)
  {
      if (fp.Addres != null)
      {
          adrTable = new JTable();
          adrr = new String[fp.Addres.size()][6];
          adressRow = fp.Addres.size();
          int count = 0;
          for (Address ad : fp.Addres) {
              adrr[count][0] = ad.Type.Type;
              adrr[count][1] = ad.country.name;
              adrr[count][2] = ad.city.name;
              adrr[count][3] = ad.street.name;
              adrr[count][4] = ad.Add;
              adrr[count][5] = ad.ID;
              count++;
          }
          AdrTable();
      }
      
  }
  
    private List<Phone> getPhones() {
        List<Phone> Phones = new ArrayList<Phone>();
        
        for (int i=0; i < phoneRow; i++)
        {
            Phone p = new Phone();
            TypePhone ta = new TypePhone();
            ta.Type = (String) phonTable.getValueAt(i, 0);
            p.Type = ta;
            p.Number = (String) phonTable.getValueAt(i, 1);
            p.ID = (String) phonTable.getValueAt(i, 2);
            Phones.add(p);
           // System.out.println(p.Type.Type + " " + p.Number);
        }
        return Phones;
    }
    
    private List<Address> getAdress() {
        List<Address> Adres = new ArrayList<Address>();
        
        for (int i=0; i < adressRow; i++)
        {
            Address p = new Address();
            TypeAdress type = new TypeAdress();
            type.Type = (String) adrTable.getValueAt(i, 0);
            p.Type = type;
            
            Country country = new Country();
            country.name = (String) adrTable.getValueAt(i, 1);
            p.country = country;
            
            City city = new City();
            city.name = (String) adrTable.getValueAt(i, 2);
            p.city = city;
            
            Street street = new Street();
            street.name = (String) adrTable.getValueAt(i, 3);
            p.street = street;
            
            p.Add = (String) adrTable.getValueAt(i, 4);
            p.ID = (String) adrTable.getValueAt(i, 5);
          
            Adres.add(p);
           // System.out.println(p.Type.Type + " " + p.Number);
        }
        return Adres;
    }
    
    private void delTelRow()
    {
      int selectedRow = phonTable.getSelectedRow();
      ((DefaultTableModel)phonTable.getModel()).removeRow(selectedRow);
      String phone2[][] = new String[phone.length-1][3];
      for (int i = 0; i < phone.length-1; i++)
      {
        for (int k=0; k < 3; k++)
        {
          if (i != selectedRow)
            phone2[i][k] = phone[i][k];
        }
      }
      phone = new String[phone.length-1][3];
      for (int i = 0; i < phone2.length-1; i++)
      {
        for (int k=0; k < 3; k++)
        {
          if (i != selectedRow)
            phone[i][k] = phone2[i][k];
        }
      }

      phoneRow--; // удалять массив с данными phone
    }
    
    private void delAdrRow()
    {
      int selectedRow = adrTable.getSelectedRow();
      ((DefaultTableModel)adrTable.getModel()).removeRow(selectedRow);
      String adrr2[][] = new String[adrr.length-1][6];
      for (int i = 0; i < adrr.length-1; i++)
      {
        for (int k=0; k < 6; k++)
        {
          if (i != selectedRow)
            adrr2[i][k] = adrr[i][k];
        }
      }
      adrr = new String[adrr.length-1][6];
      for (int i = 0; i < adrr2.length-1; i++)
      {
        for (int k=0; k < 6; k++)
        {
          if (i != selectedRow)
            adrr[i][k] = adrr2[i][k];
        }
      }

      adressRow--; // удалять массив с данными phone
    }
    
    private void saveMyPersone()
    {
      FullPersone obj2 = new FullPersone();
      if (fp != null) {
        obj2.ID = fp.ID;
        obj2.uid = fp.uid;
      }

      obj2.FName = NameFild.getText();
      obj2.LName = LastNameField.getText();
      obj2.bd = AgeField.getText();
      obj2.Phones = getPhones();
      obj2.Addres = getAdress();
      Gson gson2 = new Gson();
      String str = gson2.toJson(obj2);

      Command obj = new Command();
      Gson gson = new Gson();
      obj.Type = "Set";
      GType gt = new GType();
      gt.Type = "Person";
      obj.Data = gson.toJson(gt);
      obj.Add = str;
      String res = gson.toJson(obj);
      if (fp != null) {
        clPr.connectClient.refuse(res);
      } else {
        cli.refuse(res);
      }
    }
    
    private void validation()
    {
      if (NameFild.getText().isEmpty() || LastNameField.getText().isEmpty() || AgeField.getText().isEmpty()){
        new ErrorValidation().setVisible(true);
      }
      else {
        saveMyPersone();
      }
    }
    
    private void delPersone(FullPersone fp) {
      Command obj = new Command();
      Gson gson = new Gson();
      obj.Type = "Del";
      GType gt = new GType();
      gt.Type = "Person";
      obj.Data = gson.toJson(gt);
      obj.Add = gson.toJson(fp);;
      String res = gson.toJson(obj);
      clPr.connectClient.refuse(res);
  }

 

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        title = new java.awt.Label();
        NameFild = new java.awt.TextField();
        NameTitle = new java.awt.Label();
        LastName = new java.awt.Label();
        Age = new java.awt.Label();
        AgeField = new java.awt.TextField();
        Phone1 = new java.awt.Label();
        LastNameField = new java.awt.TextField();
        Adres = new java.awt.Label();
        SaveButton = new java.awt.Button();
        CloseButton = new java.awt.Button();
        PhonePanel = new javax.swing.JScrollPane();
        AdressPanel = new javax.swing.JScrollPane();
        ButtonAddTel = new javax.swing.JButton();
        ButtonAddAdr = new javax.swing.JButton();
        ButtonGetCity = new javax.swing.JButton();
        ButtonAddCity = new javax.swing.JButton();
        ButtonDeltel = new javax.swing.JButton();
        ButtonDelAdr = new javax.swing.JButton();
        DelButton = new java.awt.Button();
        PhotoPanel = new javax.swing.JPanel();

        setMinimumSize(new java.awt.Dimension(670, 420));

        title.setAlignment(java.awt.Label.CENTER);
        title.setBackground(new java.awt.Color(255, 255, 255));
        title.setFont(new java.awt.Font("Times New Roman", 0, 36));
        title.setText("Адресная книга");

        NameFild.setFont(new java.awt.Font("Times New Roman", 0, 18));

        NameTitle.setFont(new java.awt.Font("Times New Roman", 0, 18));
        NameTitle.setText("Имя");

        LastName.setFont(new java.awt.Font("Times New Roman", 0, 18));
        LastName.setText("Фамилия");

        Age.setFont(new java.awt.Font("Times New Roman", 0, 18));
        Age.setText("Дата рождения");

        AgeField.setFont(new java.awt.Font("Times New Roman", 0, 18));

        Phone1.setFont(new java.awt.Font("Times New Roman", 0, 18));
        Phone1.setText("Телефоны");

        LastNameField.setFont(new java.awt.Font("Times New Roman", 0, 18));

        Adres.setFont(new java.awt.Font("Times New Roman", 0, 18));
        Adres.setText("Адреса");

        SaveButton.setFont(new java.awt.Font("Times New Roman", 0, 18));
        SaveButton.setLabel("Сохранить");
        SaveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveButtonActionPerformed(evt);
            }
        });

        CloseButton.setFont(new java.awt.Font("Times New Roman", 0, 18));
        CloseButton.setLabel("Закрыть");
        CloseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CloseButtonActionPerformed(evt);
            }
        });

        PhonePanel.setBackground(new java.awt.Color(255, 255, 255));

        AdressPanel.setBackground(new java.awt.Color(255, 255, 255));

        ButtonAddTel.setText("+");
        ButtonAddTel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonAddTelActionPerformed(evt);
            }
        });

        ButtonAddAdr.setText("+");
        ButtonAddAdr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonAddAdrActionPerformed(evt);
            }
        });

        ButtonGetCity.setText("GetCity");
        ButtonGetCity.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonGetCityActionPerformed(evt);
            }
        });

        ButtonAddCity.setText("GetStreet");
        ButtonAddCity.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonAddCityActionPerformed(evt);
            }
        });

        ButtonDeltel.setText("-");
        ButtonDeltel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonDeltelActionPerformed(evt);
            }
        });

        ButtonDelAdr.setText("-");
        ButtonDelAdr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonDelAdrActionPerformed(evt);
            }
        });

        DelButton.setFont(new java.awt.Font("Times New Roman", 0, 18));
        DelButton.setLabel("Удалить");
        DelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DelButtonActionPerformed(evt);
            }
        });

        PhotoPanel.setBackground(new java.awt.Color(255, 255, 255));
        PhotoPanel.setPreferredSize(new java.awt.Dimension(100, 111));

        javax.swing.GroupLayout PhotoPanelLayout = new javax.swing.GroupLayout(PhotoPanel);
        PhotoPanel.setLayout(PhotoPanelLayout);
        PhotoPanelLayout.setHorizontalGroup(
            PhotoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        PhotoPanelLayout.setVerticalGroup(
            PhotoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 111, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(title, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 714, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(NameTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(LastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(NameFild, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(LastNameField, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(AgeField, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(Age, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(63, 63, 63)
                        .addComponent(DelButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(96, 96, 96)
                        .addComponent(Phone1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(110, 110, 110)
                        .addComponent(Adres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(AdressPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(ButtonAddAdr)
                                    .addComponent(ButtonAddCity)
                                    .addComponent(ButtonGetCity)
                                    .addComponent(ButtonDelAdr)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(PhonePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(ButtonAddTel)
                                    .addComponent(ButtonDeltel))
                                .addGap(18, 18, 18)
                                .addComponent(PhotoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(74, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(245, Short.MAX_VALUE)
                .addComponent(SaveButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(131, 131, 131)
                .addComponent(CloseButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(161, 161, 161))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(title, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(NameTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Phone1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(NameFild, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(LastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(2, 2, 2)
                        .addComponent(LastNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Age, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(AgeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(55, 55, 55)
                        .addComponent(DelButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(PhonePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(Adres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(28, 28, 28)
                                .addComponent(ButtonAddTel, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(ButtonDeltel, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(PhotoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(ButtonAddAdr, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(ButtonDelAdr, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(3, 3, 3)
                                .addComponent(ButtonGetCity)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(ButtonAddCity))
                            .addComponent(AdressPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(CloseButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(SaveButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

  private void CloseButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_CloseButtonActionPerformed
  {//GEN-HEADEREND:event_CloseButtonActionPerformed
    this.dispose();
  }//GEN-LAST:event_CloseButtonActionPerformed

  private void SaveButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_SaveButtonActionPerformed
  {//GEN-HEADEREND:event_SaveButtonActionPerformed
    validation();
  }//GEN-LAST:event_SaveButtonActionPerformed

private void ButtonAddTelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonAddTelActionPerformed
    addTell();
}//GEN-LAST:event_ButtonAddTelActionPerformed

private void ButtonAddAdrActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonAddAdrActionPerformed
    addAdr();
}//GEN-LAST:event_ButtonAddAdrActionPerformed

private void ButtonGetCityActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonGetCityActionPerformed
 AdrTable();
}//GEN-LAST:event_ButtonGetCityActionPerformed

private void ButtonAddCityActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonAddCityActionPerformed
AdrTable();
}//GEN-LAST:event_ButtonAddCityActionPerformed

  private void ButtonDeltelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonDeltelActionPerformed
    delTelRow();
  }//GEN-LAST:event_ButtonDeltelActionPerformed

  private void ButtonDelAdrActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonDelAdrActionPerformed
    delAdrRow();
  }//GEN-LAST:event_ButtonDelAdrActionPerformed

  private void DelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DelButtonActionPerformed
    delPersone(fp);
    this.dispose();
  }//GEN-LAST:event_DelButtonActionPerformed

 
  

  /**
   * @param args the command line arguments
   */
  public static void main(String args[])
  {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try
    {
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
      {
        if ("Nimbus".equals(info.getName()))
        {
          javax.swing.UIManager.setLookAndFeel(info.getClassName());
          break;
        }
      }
    } catch (ClassNotFoundException ex)
    {
      java.util.logging.Logger.getLogger(AdressBooks.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex)
    {
      java.util.logging.Logger.getLogger(AdressBooks.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex)
    {
      java.util.logging.Logger.getLogger(AdressBooks.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex)
    {
      java.util.logging.Logger.getLogger(AdressBooks.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {

      public void run()
      {
        new AdressBooks().setVisible(true);
      }
    });
  }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Label Adres;
    private javax.swing.JScrollPane AdressPanel;
    private java.awt.Label Age;
    private java.awt.TextField AgeField;
    private javax.swing.JButton ButtonAddAdr;
    private javax.swing.JButton ButtonAddCity;
    private javax.swing.JButton ButtonAddTel;
    private javax.swing.JButton ButtonDelAdr;
    private javax.swing.JButton ButtonDeltel;
    private javax.swing.JButton ButtonGetCity;
    private java.awt.Button CloseButton;
    private java.awt.Button DelButton;
    private java.awt.Label LastName;
    private java.awt.TextField LastNameField;
    private java.awt.TextField NameFild;
    private java.awt.Label NameTitle;
    private java.awt.Label Phone1;
    private javax.swing.JScrollPane PhonePanel;
    public javax.swing.JPanel PhotoPanel;
    private java.awt.Button SaveButton;
    private java.awt.Label title;
    // End of variables declaration//GEN-END:variables

    


    
}
